require.config({
    baseUrl: "script/module"
});
require(["jquery", "frameworkDom", "webSocketChannel", 'jquery.cookie', 'userHandler', 'chatAreaDom', 'chatHandler', 'courseMemberAreaDom', 'courseAreaDom', 'introduceDom', 'courseViewController', "voteModule", "liveAreaDom"],
    function ($, framewordDom, ws, cookie, userHandler, chatAreaDom, chatHandler, courseMemberAreaDom, courseAreaDom, introduceDom, viewControl, voteModule, liveAreaDom) {

        // 测试
        // $.cookie("courseId", 2);

        var stuInfo = {
                username: "test2",
                password: "test2"
            },
            tchInfo = {
                username: "gzq9425",
                password: "123"
            }
        //
        // var userHandler = new userHandler.UserHandler();
        // userHandler.login(tchInfo, function () {

            var courseId = $.cookie("courseId");
            var type = $.cookie("type");


            framewordDom.initHTML();
            framewordDom.initComponents();


            //Page 1
            // 聊天区域

            chatAreaDom.execute();
            setTimeout(function () {


                chatHandler.send({name: "lph", data: "hello, im " + Math.random()}, function () {
                    console.log("sendMedssage");
                }, function () {
                    console.log("妈呀没发出去")
                })
            }, 3000);


            // 直播区域
            liveAreaDom.execute();


            //学生管理或课程管理区域
            if (type === "student") {
                courseAreaDom.execute();
            } else {
                courseMemberAreaDom.execute();
            }

            // 课程介绍区域
            introduceDom.execute();


            //question页面


            function init() {
                viewControl.showNotice(courseId);
                viewControl.showFile(courseId);
                console.log($.cookie('type'))
                if ($.cookie('type') == 'teacher') {
                    viewControl.teacherGetVote(courseId);
                } else {
                    viewControl.studentGetVote(courseId);
                    document.getElementById('icon-1').id = '';
                    document.getElementById('icon-2').id = '';
                }

            }

            init();


            //event binding
            if (document.getElementById('icon-1')) {
                document.getElementById('icon-1').addEventListener('click', function () {
                    showMode('bulletin');
                })
                document.getElementById('icon-2').addEventListener('click', function () {
                    showMode('question');
                })
            }

            document.getElementById('icon-3').addEventListener('click', function () {

                showMode('material');
            })

            function showMode(m) {
                closeMode();
                var body = document.body;
                var div = document.createElement('div');
                div.className = 'mode';
                div.id = m;
                if (m == 'bulletin') {
                    div.innerHTML = `<img class="mode-icon" src="image/bulletin-icon.png" alt="">
        <div class="mode-head">发布公告</div>
        <textarea name="" id="mode-content" class="mode-content" placeholder="请输入公告内容"></textarea>
        <div class="mode-btn">
            <img src="image/cancel.png" alt="" onclick="closeMode()">
            <img src="image/upload.png" alt="" id="post-btn1">
        </div>`;
                    body.appendChild(div);

                    document.getElementById('post-btn1').addEventListener('click', function () {
                        viewControl.postNotice(courseId);
                        closeMode();
                    })
                } else if (m == 'question') {
                    div.innerHTML = `<img class="mode-icon" src="image/question-icon.png" alt="">
        <div class="mode-head">发布问题</div>
        <textarea name="" id="mode-content" class="mode-content" placeholder="请输入问题内容"></textarea>
        <div class="mode-btn">
            <img src="image/cancel.png" alt="" onclick="closeMode()">
            <img src="image/upload.png" alt="" id="post-btn2">
        </div>`;
                    body.appendChild(div);

                    document.getElementById('post-btn2').addEventListener('click', function () {
                        viewControl.startVote(courseId);
                        closeMode();
                    })
                } else {
                    div.innerHTML = `<img class="mode-icon" src="image/material-icon.png" alt="">
        <div class="mode-head">发布资料</div>
        <div class="mode-name" id="file-name" style="display: none"> <span class="gray-word"></span></div>
        <input type="file" id="btn_file" style="display:none" onchange="showFileDetial()">
        <div class="mode-state" id="fileShow" style="display: none"><img src="image/material-a.png" alt="">加载成功！</div>
        <div class="mode-state" id="fileUnshow" onclick="F_Open_dialog()" style="color: #7c7c7c"><img src="image/material.png"  alt="">点击上传</div>
        <div class="mode-btn">
            <img src="image/cancel.png" alt="" onclick="closeMode()">
            <img src="image/upload.png" alt="" id="post-btn3">
        </div>`;
                    body.appendChild(div);

                    document.getElementById('post-btn3').addEventListener('click', function () {
                        viewControl.uploadFile(courseId);
                        // closeMode();
                    })
                }


            }

            function closeMode() {
                var mode = document.getElementsByClassName('mode');
                for (var j = 0; j < mode.length; j++) {
                    mode[j].remove();
                }
            }

        //
        // })


    })